package com.liufei1988.design.singleton;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 懒汉式
 */
public class IdGenerator {
    private AtomicLong id=new AtomicLong(0);
    private static  IdGenerator instance=null;
    private IdGenerator(){};

    /**
     * 懒汉式
     * @return
     */
    public static synchronized IdGenerator getInstance(){
        if (instance==null){
            instance=new IdGenerator();
        }
        return instance;
    }

    /**
     * 双重检测
     * @return
     */
    public static IdGenerator getInstance1(){
        if (instance==null){
            synchronized (IdGenerator.class){
                if (instance==null){
                    instance=new IdGenerator();
                }
            }
        }
        return instance;
    }

    public long getId(){
        return id.incrementAndGet();
    }

    public static void main(String[] args) {
        IdGenerator id=new IdGenerator();
        id.getId();
        System.out.print(id.getId());
    }
}
